981. 基于时间的键值存储
为保证权益,题目请参考 981. 基于时间的键值存储(From LeetCode).
解决方案1
Python
python
# 981. 基于时间的键值存储
# https://leetcode-cn.com/problems/time-based-key-value-store/
class TimeMap:
def __init__(self):
"""
Initialize your data structure here.
"""
self.data = dict()
def set(self, key: str, value: str, timestamp: int) -> None:
if key in self.data:
self.data[key].append({
"timestamp": timestamp,
"value": value
})
else:
self.data[key] = [
{
"timestamp": timestamp,
"value": value
}
]
def get(self, key: str, timestamp: int) -> str:
if key not in self.data:
return ""
# 最好的方式是采用二分查找z在有序列表找到内容
l = 0
r = len(self.data[key]) - 1
li = self.data[key]
while l <= r:
m = (l+r) // 2
if li[m]["timestamp"] <= timestamp:
if m + 1 < len(li):
if li[m+1]["timestamp"] > timestamp:
break
else:
l = m+1
else:
break
else:
r = m-1
if l <= r:
return li[m]["value"]
else:
return ""
if __name__ == "__main__":
obj = TimeMap()
obj.set("a", "aaa1", 1)
obj.set("a", "aaa2", 12)
obj.set("a", "aaa3", 13)
obj.set("a", "aaa4", 123)
print(obj.get("a", 123))
# Your TimeMap object will be instantiated and called as such:
# obj = TimeMap()
# obj.set(key,value,timestamp)
# param_2 = obj.get(key,timestamp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66